﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Text;
using Xpom_Xpum__Integrated_Development_Environment.Model.Controls;
using Xpom_Xpum__Integrated_Development_Environment.ViewModel;

namespace Xpom_Xpum__Integrated_Development_Environment.ViewModel.Controls
{
    public class TabControlViewModel : ViewModel
    {
        #region Constructor

        public TabControlViewModel()
        {
            ta = new ObservableCollection<TabItemModel>();
            SelectedTabIndex = 0;
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        private int selectedTabIndex;

        #endregion

        #region Properties

        public ObservableCollection<TabItemModel> ta { get; set; }

        public int SelectedTabIndex
        {
            get
            {
                return selectedTabIndex;
            }
            set
            {
                selectedTabIndex = value;
                OnPropertyChanged("SelectedTabIndex");
            }
        }

        #endregion

        #region Methods

        public void Close(string path)
        {
            TabItemModel found = null;

            foreach (var tim in ta)
            {
                if (tim.Path == path)
                {
                    found = tim;
                }
            }

            if (found != null)
            {
                ta.Remove(found);

                if (ta.Count == 0)
                    ((App)App.Current).mvm.IsFileOpened = false;
            }
        }

        public void Close(TabItemModel tab)
        {
            if (ta.IndexOf(tab) >= 0)
            {
                ta.Remove(tab);

                if (ta.Count == 0)
                    ((App)App.Current).mvm.IsFileOpened = false;
            }
        }

        public void CloseAll()
        {
            while (ta.Count > 0)
            {
                ta.RemoveAt(0);
            }

            ((App)App.Current).mvm.IsFileOpened = false;
        }

        public void CloseCurrent()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                if (current != null)
                {
                    ta.Remove(current);

                    System.Windows.MessageBox.Show(ta.Count.ToString());

                    if (ta.Count == 0)
                        ((App)App.Current).mvm.IsFileOpened = false;
                }
            }
        }

        public TabItemModel GetTabItemByPath(string filePath)
        {
            TabItemModel result = null;

            foreach (var tim in ta)
            {
                if (tim.Path == filePath)
                {
                    result = tim;
                    break;
                }
            }

            return result;
        }

        public void LoadFile(string filePath)
        {
            var existTabItem = GetTabItemByPath(filePath);

            if (existTabItem == null)
            {
                var fileContent = "";

                fileContent = File.ReadAllText(filePath);

                ta.Add(new TabItemModel()
                {
                    Code = fileContent,
                    Header = Path.GetFileName(filePath),
                    Path = filePath
                });

                SelectedTabIndex = ta.Count - 1;

                ((App)App.Current).mvm.IsFileOpened = true;
            }
            else
            {
                SelectedTabIndex = ta.IndexOf(existTabItem);
            }
        }

        public void Undo()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Undo();
            }
        }

        public void Redo()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Redo();
            }
        }

        public void Cut()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Cut();
            }
        }

        public void Copy()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Copy();
            }
        }

        public void Paste()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Paste();
            }
        }

        public void Delete()
        {
            if (SelectedTabIndex < ta.Count)
            {
            var current = ta[SelectedTabIndex];
            current.Delete();
        }
        }

        public void SelectAll()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.SelectAll();
            }
        }

        public void Save(TabItemModel tab)
        {
            Save(tab, tab.Path);
        }

        public void Save(TabItemModel tab, string path)
        {
            File.WriteAllText(path, tab.Code);
        }

        public void SaveAll()
        {
            foreach (var tab in ta)
            {
                Save(tab);
            }
        }

        //public void SaveAllAs(string newProjectPath) { }

        public void SaveCurrent()
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                Save(current);
            }
        }

        public void SaveCurrentAs(string path)
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                Save(current, path);
            }
        }

        public void FindCurrent(string findWhat, bool findPrevious = false,
            bool matchCase = false, bool matchWholeWord = false, bool useWildcards = false)
        {
            if (SelectedTabIndex < ta.Count)
            {
                var current = ta[SelectedTabIndex];
                current.Find(findWhat, findPrevious, matchCase, matchWholeWord, useWildcards);
            }
        }

        public void FindNextCurrent()
        {
        }

        public void FindPreviousCurrent()
        {
        }

        public void ReplaceCurrent()
        {
        }

        public void ReplaceAllCurrent()
        {
        }

        #endregion
    }
}
